package org.jeecg.modules.demo3.peijian.service.impl;



import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.jeecg.modules.demo3.peijian.entity.AxhkPjClq;
import org.jeecg.modules.demo3.peijian.entity.AxhkXxwyLinearRegulato;
import org.jeecg.modules.demo3.peijian.entity.materials.Rental;
import org.jeecg.modules.demo3.peijian.mapper.AxhkXxwyLinearRegulatoMapper;
import org.jeecg.modules.demo3.peijian.service.IAxhkXxwyLinearRegulatoService;
import org.jeecg.modules.demo3.peijian.utill.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @Description: 线性稳压
 * @Author: jeecg-boot
 * @Date:   2021-04-13
 * @Version: V1.0
 */
@Service
public class AxhkXxwyLinearRegulatoServiceImpl extends ServiceImpl<AxhkXxwyLinearRegulatoMapper, AxhkXxwyLinearRegulato> implements IAxhkXxwyLinearRegulatoService {

    @Autowired
    private AxhkXxwyLinearRegulatoMapper axhkXxwyLinearRegulatoMapper;

    //计算所有 现有数量 计算所有购买金额
    @Override
    public Rental AtAxhkXxwyLinearRegulato() {
        List<Map<String,Object>> axhkXxwyLinearRegulato = axhkXxwyLinearRegulatoMapper.getAxhkXxwyLinearRegulato();
        Rental rental = new Rental();
        int number = 0;//剩余总数量
        Double retal = 0.0;// 一栏 总金额
        for(int i = 0;i<axhkXxwyLinearRegulato.size();i++){
            Integer h = (Integer) axhkXxwyLinearRegulato.get(i).get("price_per_num");
            Double r = (Double)axhkXxwyLinearRegulato.get(i).get("price_per_unit");
            String n = (String)axhkXxwyLinearRegulato.get(i).get("now_number");
            if(null != r && null != h){
                Integer num = (int) axhkXxwyLinearRegulato.get(i).get("price_per_num");//购买数量
                Double reta = (Double)axhkXxwyLinearRegulato.get(i).get("price_per_unit");//购买单价
                // 购买总价格 Double.parseDouble
                retal = retal+(num*reta);
            }
            if(null != n){
                String numb = (String)axhkXxwyLinearRegulato.get(i).get("now_number");//现有数量
                //有些数值从在 ≥290
                String a = numb.substring(0,1);
                //判断如有 范围数值 一切按 =数值计算
                if(a.equals("≥")){
                    String b = numb.substring(1,numb.length());
                    int numbers = Integer.valueOf(b).intValue();
                    // 现有总数量
                    number = number+numbers;
                }else {
                    int numbers = Integer.valueOf(numb).intValue();
                    // 现有总数量
                    number = number+numbers;
                }
            }
        }
        //获取小数点后两位
        BigDecimal b = BigDecimal.valueOf(retal).setScale(2, BigDecimal.ROUND_HALF_UP);
        Double retals = b.doubleValue();
        //进行添加
        rental.setNumber(number);
        rental.setRetals(retals);
        return rental ;
    }

    //点击移除 根据id 删除数据 添加数据
    //电阻（num=1）\电容（num=2）\电感（num=3）\二极管/晶体管（num=4）
    //场效应管（num=5）\电源芯片（num=6）\芯片（num=7）\处理器（num=8）
    //接插件（num=9）\其他（num=10）
    @Override
    public int AtLinearRegulato(String id, int num) {
        int a = 0;
        ObjectMapper objectMapper = new ObjectMapper();
        AddObject addObject = new AddObject();
        //当用户点击移除信息的时候 查取该用户移除的信息
        AxhkXxwyLinearRegulato axhkXxwyLinearRegulato = axhkXxwyLinearRegulatoMapper.getSelectLinearRegulato(id);
        //对移除的信息 进行添加
        int i = addObject.getAdd(axhkXxwyLinearRegulato,num);
        if(i>0){
            //本表中删除移除的信息
            a = axhkXxwyLinearRegulatoMapper.getDeleteLinearRegulato(id);
        }
        return a;
    }

    //上传文件
    @Override
    public int AtUpdateAxhkXxwyLinearRegulato(String id,String names) {
        int i = 0;
        String paths = dataPath(id);
        if(null != paths){ //有文件
            PdfUtill.pub(id); // 数据库有文件直接删除
            i = 0;
        }else{ //没有文件
            String path = "C:\\PDF\\fly\\axhk\\";
            String oldName = PdfUtill.getPdfName(); //获取的文件名
            String newName = id+oldName;
            boolean chang = PdfUtill.getByfileName(path,oldName,newName); //对上传的文件名修改
            if(chang == true){
                String newPath = "C:\\PDF\\fly\\axhk\\"+names;
                String pats = newPath+"\\"+newName; //  E:\PDF\fly\axhk\某个配件\id+文件名
                i = axhkXxwyLinearRegulatoMapper.getUpdateAxhkXxwyLinearRegulato(id,pats); //数据库储存
                if(i>0){ //数据库保存成功
                    PdfUtill.copyFile(newPath,id);
                }else{ //数据库保存未成功
                    PdfUtill.pub(id);
                }
            }else{
                i = 0;
            }
        }
        return i;
    }

    //点击删除 通过id 修改path 并且删除本地文件
    @Override
    public int AtuUpdateXxwyLinearRegulatoPath(String id) {
        int i = 0;
        //首先查询此ID下是否有文件
        String path = dataPath(id);
        if(null != path){
            DeleteFile deleteFiles = new DeleteFile();
            int a = deleteFiles.deleteFile(path);
            if(a == 1){
                path = null;
                i = axhkXxwyLinearRegulatoMapper.getUpdateAxhkXxwyLinearRegulato(id,path);
            }else{
                i = 0;
            }
        }else{
            i = 2;
        }
        return i;
    }


    //浏览器查看上传pdf文件
    @Override
    public String dataPath(String id) {
        AxhkXxwyLinearRegulato axhkXxwyLinearRegulato = axhkXxwyLinearRegulatoMapper.getSelectLinearRegulato(id);
        String path = axhkXxwyLinearRegulato.getPath();
        return path;
    }
}
